﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using KLS.PPDS.Utilities;
using KLS.PPDS.Utility;

namespace KLS.PPDS.Utilities.BaseController
{
    [HandleError]
    //[AjaxAuthorize]
    public class ControllerBase : Controller
    {
        /// <summary>
        /// This is controller common error handler
        /// for showing error message please use OnComplete/OnSucess method to receve json error message
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {

            if (filterContext == null)
            {
                return;
            }
            if (filterContext.Exception != null)
            {
                filterContext.Exception.ErrorLog();
            }

            if ((filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception != null) || filterContext.Exception != null)
            {
                filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                //filterContext.Result = new JavaScriptResult() { Script = "alert(hi);" };

                filterContext.Result = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    // ContentType = "application/json; charset=utf-8",
                    Data = new
                    {
                        Message = filterContext.Exception.Message,
                        StackTrace = filterContext.Exception.StackTrace,
                        Result = true
                    }
                };
                filterContext.ExceptionHandled = true;
            }
            //filterContext.RequestContext.HttpContext.Response.R
            //base.OnException(filterContext);
        }
    }
}
